import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import postCssPxToRem from 'postcss-pxtorem'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    Components({
      resolvers: [VantResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    // css 预处理器
    preprocessorOptions: {
      less: {
        charset: false,
        additionalData: '@import "./src/assets/css/index.less";'
      }
    },
    // rem适配
    postcss: {
      plugins: [
        postCssPxToRem({
          rootValue: 37.5, // 1rem的大小
          propList: ['*'] // 需要转换的属性，这里选择全部都进行转换
        })
      ]
    }
  },
  server: {
    proxy: {
      // 向网易严选发送请求的代理
      '/wangyi': {
        target: 'https://m.you.163.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/wangyi/, '')
      },
      // 向尚品汇前台服务器发送请求的代理
      '/sph': {
        target: 'http://sph-h5-api.atguigu.cn',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/sph/, '')
      }
    }
  }
})
